package servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import po.Account;
import po.Response;
import service.AccountService;
import service.CartService;
import service.impl.AccountServiceImpl;
import service.impl.CartServiceImpl;
import util.CryptographyUtil;
import util.ResponseUtil;
import util.UUIDUtil;

public class AccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AccountService accountService = new AccountServiceImpl();
	private CartService cartService = new CartServiceImpl();
    public AccountServlet() {
        super();
       
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		String action = request.getParameter("action");
		if(action.equals("login")){
			login(request,response);
		}else if(action.equals("checkAccount")){
			checkName(request,response);
		}else if(action.equals("register")){
			register(request,response);
		}else if(action.equals("logout")){
			logout(request,response);
		}
	}

	private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		session.invalidate();
		response.sendRedirect(request.getContextPath() + "/index.jsp");
		
	}

	private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		password = CryptographyUtil.md5(password, "tmac");
		HttpSession session = request.getSession();
		String UUID = UUIDUtil.getUUID();
		Account account = new Account();
		account.setAid(UUID);
		account.setUsername(username);
		account.setPassword(password);
		Integer count = accountService.addAccount(account);
		if(count == 1){
			session.setAttribute("account", account);
			response.sendRedirect(request.getContextPath() + "/index");
		}
	}

	private void checkName(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		boolean flag = accountService.checkUsername(username);
		Response res = new Response();
		if(flag){
			res.setStatus(true);
			res.setMessage("用户已经存在");
		}else{
			res.setStatus(false);
			res.setMessage("用户可以使用");
		}
		try {
			ResponseUtil.write(res, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		password = CryptographyUtil.md5(password, "tmac");
		Account account = accountService.login(username, password);
		Response res = new Response();
		if(account != null){
			res.setStatus(true);
			request.getSession().setAttribute("account", account);
			Map<Integer,Integer> carts = cartService.findCartByAid(account.getAid());
			request.getSession().setAttribute("carts", carts);
		}else
			res.setStatus(false);
		try {
			ResponseUtil.write(res, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
